<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" :model="listQueryForm" label-width="100px">
        <el-row :gutter="10">
          <!-- <el-col :span="12" :lg="8">
            <el-form-item prop="strName" label="设备类型">
              <el-input v-model="listQueryForm.strName" placeholder="请输入设备类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :lg="8">
            <el-form-item prop="strValue" label="基础应用">
              <el-input v-model="listQueryForm.strValue" placeholder="请输入基础应用" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :lg="8">
            <el-form-item prop="code" label="设备类型码">
              <el-input v-model="listQueryForm.code" placeholder="请输入设备类型码" />
            </el-form-item>
          </el-col>-->
          <el-col :span="12" :lg="8">
            <el-button type="primary" @click="dialogPvVisible = true">添加</el-button>
            <!-- <el-button type="primary" @click="fetchList">搜索</el-button> -->
            <!-- <el-button type="primary" @click="$refs['form'].resetFields()">重置</el-button> -->
            <!-- <el-button type="danger" @click="handleDeleteMultiple">删除</el-button> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table :data="tableData" style="width: 100%" border @selection-change="handleSelect">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="80" />
      <el-table-column prop="STR_VALUE" label="设备类型名称" width="180" />
      <!-- <el-table-column prop="STR_APPNAME" label="设备分辨率" width="180" /> -->
      <el-table-column prop="STR_APPNAME" label="基础应用" width="220">
        <template slot-scope="{row}">
          <el-tag v-for="(item, index) in row.STR_APPNAME" :key="index" style="margin-left: 10px">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="STR_CODE" label="设备类型码">
        <!-- <template slot-scope="{row}">
          <el-tag v-for="(item, index) in row.typeCode" :key="index">{{ item }}</el-tag>
        </template>-->
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <!-- <el-button size="mini" type="primary" @click="handleModifyStatus(row)">修改</el-button> -->
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQueryForm.pagenum"
      layout="total, prev, pager, next, jumper"
      :limit.sync="limit"
      @pagination="fetchList"
    />

    <el-dialog
      :visible.sync="dialogPvVisible"
      :append-to-body="true"
      :title="diaTitle"
      :data-case="dataCase"
      :close-on-click-modal="false"
    >
      <div>
        <el-form
          ref="ruleForm"
          :model="form"
          :rules="rules"
          label-position="right"
          label-width="110px"
        >
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item prop="strValue" label="设备类型名称">
                <el-input v-model="form.strValue" placeholder="请输入设备类型名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="resolvingCode" label="设备分辨率">
                <el-select v-model="form.resolvingCode" placeholder="请选择设备分辨率">
                  <el-option
                    v-for="item in optionResoltion"
                    :key="item.id"
                    :label="item.strValue"
                    :value="item.strCode"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item prop="refDevice" label="选择基础应用">
                <el-checkbox-group v-model="form.refDevice">
                  <el-checkbox
                    v-for="item in baseAppType"
                    :key="item.id"
                    :label="item.iAppnum"
                  >{{ item.strAppname }}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle('ruleForm')">取消</el-button>
        <el-button v-if="dataCase === 1" type="primary" @click="handleConfirm()">确认</el-button>
        <el-button v-else type="primary" @click="handleConfirm()">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteDev,
  getBaseApp,
  addDevType,
  findDevicetype,
  getSysType,
  modifyDevType
} from '../../api/addTag'
import Pagination from '@/components/Pagination'
import pages from '@/mixins/pages'
import { validateValue } from '@/utils/validateForm'

export default {
  name: 'DeviceType',
  components: { Pagination },
  mixins: [pages],
  props: {},
  data() {
    return {
      dialogPvVisible: false,
      myOption: '新增设备类型',
      diaTitle: '新增设备类型',
      dataCase: 1,
      form: {
        strValue: '',
        resolvingCode: '',
        refDevice: [],
        strName: 'DeviceType',
        strDesc: '设备类型',
        iType: '7'
      },
      optionResoltion: [],
      listQueryForm: {
        strName: '',
        strValue: '',
        code: '',
        iType: 7,
        pagenum: 1
      },
      limit: 5,
      pagenum: 1,
      imageUrl: '',
      total: 0,
      baseAppType: [],
      tableData: [],
      rules: {
        strValue: [
          {
            required: true,
            validator: validateValue,
            trigger: 'blur'
          }
        ],
        resolvingCode: [
          { required: true, message: '设备分辨率', trigger: 'change' }
        ],
        refDevice: [
          { required: true, message: '请选择基础应用', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.fetchList()
    this.getDefault()
  },
  mounted() {},
  methods: {
    getDefault() {
      // 获取基础应用
      getBaseApp({ iApptype: -1 })
        .then(res => {
          this.baseAppType = res.data
        })
        .catch(err => {
          console.log(err)
        })
      getSysType({ typeInfo: [2] })
        .then(res => {
          this.optionResoltion = res.data[2]
        })
        .catch(err => {
          console.log(err)
        })
    },
    fetchList() {
      findDevicetype(this.listQueryForm)
        .then(res => {
          this.tableData = res.data
          this.total = !res.total ? 0 : res.total
        })
        .catch(err => {
          console.log(err)
        })
    },
    handleModifyStatus(row) {
      this.diaTitle = '修改设备类型'
      this.dataCase = 0
      // this.dialogPvVisible = true;
      this.form.strValue = row.STR_VALUE
      this.form.resolving = '2'
      this.form.refDevice.push({
        number: row.I_NUMBER,
        id: row.ID
      })
      // this.form.refDevice.push(row.I_NUMBER)
    },
    handleSelect(val) {
      this.multipleSelection = val
    },
    handleDeleteMultiple() {
      if (this.multipleSelection.length > 0) {
        const idInfo = []
        this.multipleSelection.filconst(item => {
          idInfo.push(item.ID)
        })
        this.$confirm('确认删除吗？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(_ => {
          deleteDev({ idInfo }).then(res => {
            this.$notify({
              message: '操作成功',
              type: 'success'
            })
            this.ResetCurrentPage()
            this.fetchList()
          })
        })
      } else {
        this.$message({
          type: 'warning',
          title: '提示',
          message: '请先选择'
        })
      }
    },
    handleDelete(row) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        deleteDev({ idInfo: [row.ID] })
          .then(res => {
            this.$notify({
              title: '操作成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.ResetCurrentPage()
            this.fetchList()
          })
          .catch(err => {
            console.log(err)
          })
      })
    },
    handleCancle(ruleForm) {
      this.dialogPvVisible = false
      this.$refs[ruleForm].resetFields()
    },
    handleConfirm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          addDevType(this.form)
            .then(res => {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.$refs['ruleForm'].resetFields()
              this.fetchList()
            })
            .catch(err => {
              console.log(err)
            })
          this.dialogPvVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    dialogModify() {
      modifyDevType(this.form).then(res => {
        console.log(res)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.marginTop {
  margin-top: 20px;
}
</style>
